<template>
  <div class="records-container">
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="学员姓名">
          <el-input v-model="searchForm.studentName" placeholder="请输入学员姓名" clearable />
        </el-form-item>
        <el-form-item label="教练姓名">
          <el-input v-model="searchForm.coachName" placeholder="请输入教练姓名" clearable />
        </el-form-item>
        <el-form-item label="培训科目">
          <el-select v-model="searchForm.subject" placeholder="请选择科目" clearable>
            <el-option label="科目一" value="科目一" />
            <el-option label="科目二" value="科目二" />
            <el-option label="科目三" value="科目三" />
            <el-option label="科目四" value="科目四" />
          </el-select>
        </el-form-item>
        <el-form-item label="考试状态">
          <el-select v-model="searchForm.examStatus" placeholder="请选择状态" clearable>
            <el-option label="未考试" value="未考试" />
            <el-option label="已通过" value="已通过" />
            <el-option label="未通过" value="未通过" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>学习记录</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>添加记录
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="studentName" label="学员姓名" />
        <el-table-column prop="coachName" label="教练姓名" />
        <el-table-column prop="subject" label="培训科目" />
        <el-table-column prop="trainingTime" label="培训时长(小时)" width="120" />
        <el-table-column prop="trainingDate" label="培训日期" />
        <el-table-column prop="examDate" label="考试日期" />
        <el-table-column prop="examStatus" label="考试状态">
          <template #default="{ row }">
            <el-tag :type="getExamStatusType(row.examStatus)">
              {{ row.examStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="考试成绩" width="100" />
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button-group>
              <el-button type="primary" link @click="handleEdit(row)">
                <el-icon><Edit /></el-icon>编辑
              </el-button>
              <el-button type="danger" link @click="handleDelete(row)">
                <el-icon><Delete /></el-icon>删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogType === 'add' ? '添加记录' : '编辑记录'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="学员" prop="studentId">
          <el-select v-model="form.studentId" placeholder="请选择学员">
            <el-option
              v-for="student in studentList"
              :key="student.id"
              :label="student.name"
              :value="student.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="教练" prop="coachId">
          <el-select v-model="form.coachId" placeholder="请选择教练">
            <el-option
              v-for="coach in coachList"
              :key="coach.id"
              :label="coach.name"
              :value="coach.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="培训科目" prop="subject">
          <el-select v-model="form.subject" placeholder="请选择科目">
            <el-option label="科目一" value="科目一" />
            <el-option label="科目二" value="科目二" />
            <el-option label="科目三" value="科目三" />
            <el-option label="科目四" value="科目四" />
          </el-select>
        </el-form-item>
        <el-form-item label="培训时长" prop="trainingTime">
          <el-input-number
            v-model="form.trainingTime"
            :min="0"
            :max="24"
            :precision="1"
            :step="0.5"
          />
        </el-form-item>
        <el-form-item label="培训日期" prop="trainingDate">
          <el-date-picker
            v-model="form.trainingDate"
            type="date"
            placeholder="请选择培训日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="考试日期" prop="examDate">
          <el-date-picker
            v-model="form.examDate"
            type="date"
            placeholder="请选择考试日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="考试状态" prop="examStatus">
          <el-select v-model="form.examStatus" placeholder="请选择状态">
            <el-option label="未考试" value="未考试" />
            <el-option label="已通过" value="已通过" />
            <el-option label="未通过" value="未通过" />
          </el-select>
        </el-form-item>
        <el-form-item label="考试成绩" prop="score">
          <el-input-number
            v-model="form.score"
            :min="0"
            :max="100"
            :disabled="form.examStatus === '未考试'"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Search,
  Refresh,
  Plus,
  Edit,
  Delete
} from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  studentName: '',
  coachName: '',
  subject: '',
  examStatus: ''
})

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    studentName: '张三',
    coachName: '李教练',
    subject: '科目二',
    trainingTime: 2,
    trainingDate: '2024-02-20',
    examDate: '2024-02-25',
    examStatus: '已通过',
    score: 90
  }
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 表单
const dialogVisible = ref(false)
const dialogType = ref('add')
const formRef = ref(null)
const form = reactive({
  studentId: '',
  coachId: '',
  subject: '',
  trainingTime: 0,
  trainingDate: '',
  examDate: '',
  examStatus: '未考试',
  score: 0,
  remark: ''
})

// 学员列表
const studentList = ref([
  { id: 1, name: '张三' },
  { id: 2, name: '李四' }
])

// 教练列表
const coachList = ref([
  { id: 1, name: '李教练' },
  { id: 2, name: '王教练' }
])

// 表单验证规则
const rules = {
  studentId: [{ required: true, message: '请选择学员', trigger: 'change' }],
  coachId: [{ required: true, message: '请选择教练', trigger: 'change' }],
  subject: [{ required: true, message: '请选择培训科目', trigger: 'change' }],
  trainingTime: [{ required: true, message: '请输入培训时长', trigger: 'blur' }],
  trainingDate: [{ required: true, message: '请选择培训日期', trigger: 'change' }],
  examStatus: [{ required: true, message: '请选择考试状态', trigger: 'change' }]
}

// 监听考试状态变化
watch(() => form.examStatus, (newVal) => {
  if (newVal === '未考试') {
    form.score = 0
  }
})

// 获取考试状态类型
const getExamStatusType = (status) => {
  const statusMap = {
    '未考试': 'info',
    '已通过': 'success',
    '未通过': 'danger'
  }
  return statusMap[status] || 'info'
}

// 搜索
const handleSearch = () => {
  // TODO: 实现搜索逻辑
  console.log('search:', searchForm)
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  Object.keys(form).forEach(key => {
    form[key] = key === 'examStatus' ? '未考试' : key === 'trainingTime' || key === 'score' ? 0 : ''
  })
}

// 编辑
const handleEdit = (row) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  // TODO: 根据ID获取详细信息
  Object.keys(form).forEach(key => {
    form[key] = row[key]
  })
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确认删除该记录吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 实现删除逻辑
    ElMessage.success('删除成功')
  }).catch(() => {})
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现提交逻辑
      console.log('submit:', form)
      ElMessage.success(dialogType.value === 'add' ? '添加成功' : '修改成功')
      dialogVisible.value = false
    }
  })
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.records-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>